<template>
  <el-container class="admin">
    <el-aside width="200px">
      <el-radio-group v-model="isCollapse" style="margin-bottom:20px;">
        <el-radio-button :label="false">展开</el-radio-button>
        <el-radio-button :label="true">收起</el-radio-button>
      </el-radio-group>
      <el-menu
        :default-active="this.$route.fullPath==='/admin'?'/admin/dashBoard':this.$route.fullPath"
        class="el-menu-vertical-demo"
        :collapse="isCollapse"
        :router="true"
      >
        <el-menu-item v-for="nav in nvaRouter" :key="nav.path" :index="nav.path">
          <i :class="nav.icon"></i>
          <span slot="title">{{nav.name}}</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <el-dropdown style="margin-right:20px">
          <span class="el-dropdown-link">
            <span><el-avatar :src="this.userInfo.avatar"></el-avatar></span>
            <span>{{this.userInfo.userName}}</span>
            <span><i class="el-icon-arrow-down el-icon--right"></i></span>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-badge value="new" @click.native="goMessage" class="item" :hidden="!this.msg_Read()">
              <el-dropdown-item >消息中心</el-dropdown-item>
            </el-badge>
            <el-dropdown-item @click.native="eidt">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      isCollapse: false,
      nvaRouter: [],
      isRead: false,
      userInfo: {}
    }
  },
  created () {
    this.userInfo = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')):{}
  },
  mounted () {
    this.getAnvaRouter()
  },
  computed: {
    ...mapState(['msgLists'])
  },
  methods: {
    eidt () {
      localStorage.removeItem('userInfo')
      localStorage.removeItem('accessToken')
      this.$message({
        message: '退出成功',
        duration: 1000,
        type: 'success',
        onClose: () => {
          this.$router.go(0)
        }
      })
    },
    goMessage () {
      this.$router.push('/admin/message')
    },
    msg_Read () {
      return this.msgLists.some(msg => !msg.read)
    },
    getAnvaRouter () {
      const adminRoute = this.$router.options.routes.filter(
        (route) => route.admin
      )
      const childrenRoute = adminRoute[0].children
      this.nvaRouter = childrenRoute.filter((route) => route.meta.isNav)
    }
  },
  errorHandler () {
    return true
  }
}
</script>

<style lang="less" scoped>
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
  }
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.admin {
  height: 100vh;
}
.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}
body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.el-menu-vertical-demo {
  .el-menu--collapse {
    width: 200px;
    min-height: 400px;
  }
}
.el-menu-item {
  text-align: left;
}
.el-dropdown-link {
  span{
    vertical-align: middle;
    height:40px;
    line-height: 40px;
    margin:0 2px;
  }
}

</style>
